import 'package:flutter/material.dart';
import 'package:ultralytics_yolo/yolo_view.dart';
import 'package:ultralytics_yolo/yolo.dart';
import 'package:WherFire/model_manager.dart';


void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'WherFire',
      theme: ThemeData(
        useMaterial3: true,
        colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xA7B28EFF)),
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          SizedBox(height: 100),
          Text('欢迎加入NeuroMatrix_Lab',
            style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold)),
          SizedBox(height: 50),
          Text('本app为编译成功的第一个Android版本'),
          SizedBox(height: 50),
          Center(
            child: ElevatedButton(
              onPressed: (){
                Navigator.push(context, MaterialPageRoute(builder: (context){
                  return CameraDetectionScreen();
                },));
              },
              child: Text('进入WherFire测试页面1'),
            ),
          ),
          Center(
            child: ElevatedButton(
              onPressed: (){
                Navigator.push(context, MaterialPageRoute(builder: (context){
                  return YOLOExample();
                },));
              }, child: Text('进入WherFire测试页面2')
            ),
          ),
        ],
      ),
    );
  }
}

class CameraDetectionScreen extends StatefulWidget {
  @override
  _CameraDetectionScreenState createState() => _CameraDetectionScreenState();
}

class _CameraDetectionScreenState extends State<CameraDetectionScreen> {
  late YOLOViewController controller;
  List currentResults = [];

  @override
  void initState() {
    super.initState();
    controller = YOLOViewController();

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          YOLOView(
            modelPath: model_load.$modelPath,
            task: YOLOTask.detect,
            controller: controller,
            useGpu: true,
            onResult: (results) {
              print('YOLO results: $results');
              setState(() {
                currentResults = results;
              });
            },
          ),
          Positioned(
            bottom: 50,
            left: 50,
            child: Container(
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: Colors.black54,
                borderRadius: BorderRadius.circular(10),
              ),
              child: Text(
                '检测到目标数: ${currentResults.length}',
                style: TextStyle(color: Colors.white, fontSize: 18),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class YOLOExample extends StatelessWidget {
@override
  Widget build(BuildContext context) {
    return YOLOView(
      modelPath: model_load.$modelPath,
      task: YOLOTask.detect,
      useGpu: true,
      onResult: (results) {
        print('检测到 ${results.length} 个目标');
        for (final result in results) {
          print('${result.className}: ${result.confidence}');
        }
      },
    );
  }
}

